﻿using SqlSugar_Geometry_Model.Entity;
using SqlSugar_Geometry_Model.MyClass;
using SqlSugar_Geometry_Model.Service;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SqlSugar_Geometry_Model.Ashx
{
    /// <summary>
    /// SaveTest 的摘要说明
    /// </summary>
    public class SaveTest : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            new DbHelper().CreateDataBase();  //初始化数据库
            //创建随机数生成器，用于随机产生经纬度、海拔信息
            Random ran = new Random();
            //初始化Test对象
            Test test = new Test();
            //Test MingCheng 名称字段赋值
            test.MingCheng = "测试名称";
            //Test CenterShape 中心点坐标赋值   （点）
            test.CenterShape = new Location(BaseClass.RandomDouble(ran, 118.60, 118.65), BaseClass.RandomDouble(ran, 36.60, 36.65), BaseClass.RandomInt(ran, 0, 100));
            //Test RoadShape 道路坐标赋值   （线）
            Location[] linepoits = new Location[5];
            for (int a = 0; a < linepoits.Length; a++)
            {
                linepoits[a] = new Location(BaseClass.RandomDouble(ran, 118.33, 119.45), BaseClass.RandomDouble(ran, 36.33, 36.45), BaseClass.RandomInt(ran, 0, 100));
            }
            test.RoadShape = new Line(linepoits);
            //Test BorderShape 边界坐标赋值   （面）
            Location[] poits = new Location[10];
            for (int a = 0; a < poits.Length; a++)
            {
                poits[a] = new Location(BaseClass.RandomDouble(ran, 118.33, 119.45), BaseClass.RandomDouble(ran, 36.33, 36.45), BaseClass.RandomInt(ran, 0, 100));
            }
            poits[9] = poits[0];  //Polygon数据必须首尾一致
            test.BorderShape = new Polygon(poits);
            //添加数据
            int i = new DbService().saveTest(test);  
            context.Response.Write("Id为" + i + "的数据添加成功！");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}